<?php
defined('BASEPATH') OR exit('No direct script access allowed');

//训练计划
class Xunlian extends MY_Controller {

	function __construct(){
		parent::__construct();
		
	}
	
	
	//start
	/**
	* @name 获取训练计划
	* @param uid false int 会员ID
	* @param name false char 名称
	* @return 
	*/
	//end
	function get_list(){
		$user_id = $this->user_id;
		$this->load->model('Sport/CourseTrainModel','CourseTrain');
		
		$page = $this->input->post('page',null,1);
		$size = $this->input->post('size',null,10);
		
		
		
		$arr = array(
			'uid'=>$this->input->post('uid',null,0),
			'name'=>$this->input->post('name',null,''),
			'coach_id'=>$user_id
		);
		$res = $this->CourseTrain->get_list($arr,$page,$size);
		
		$this->data_out($res);
	}
	
	//start
	/**
	* @name 获取训练信息
	* @param id true int 计划ID
	* @return 
	*/
	//end
	function get_info(){
		$user_id = $this->user_id;
		
		
		$this->load->model('Sport/CourseTrainModel','CourseTrain');
		
		$id = $this->input->post('id',null,0);
		$info = $this->CourseTrain->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$info['addtime'] = date('Y-m-d H:i:s',$info['addtime']);
		
		$this->load->model('Sport/CourseTrainInfoModel','Traininfo');
		$datas = $this->Traininfo->tid_get_data($id);
		
		
		$this->load->model('Sport/SportModel','Sport');
		$this->load->model('Sport/CourseModel','Course');
		
		
		$ls_list = array();
		foreach($datas as $key=>$val){
			if($val['type']=='yinshi'){
				$ls_info = json_decode($val['info'],true);
			}
			elseif($val['type']=='dongzuo'){
				$sp_list = json_decode($val['info'],true);

				$ls_info = array();
				if($sp_list){
					foreach($sp_list as $k=>$v){
						if(!isset($v['id'])){
							continue;
						}
						$spinfo = $this->Sport->_id_get_info($v['id']);
						if($spinfo){
							$spinfo['rest'] = isset($v['rest'])?$v['rest']:'';
							$spinfo['duration'] = isset($v['duration'])?$v['duration']:'';
							$spinfo['frequency'] = isset($v['frequency'])?$v['frequency']:'';
							$spinfo['group'] = isset($v['group'])?$v['group']:'';
							
							$ls_info[] = $spinfo;
						}
					}
				}
			}
			elseif($val['type']=='kecheng'){
				$ids = json_decode($val['info'],true);
				if(!empty($ids)){
					$ls_info = $this->Course->ids_get_data($ids);
				}
			}
			
			$ls_list[] = array(
				'stime'=>$val['stime'],
				'type'=>$val['type'],
				'info'=>$ls_info
			);
		}
		

		$info['sport_list'] = $ls_list;
		
		$this->data_out($info);
	}
	//start
	/**
	* @name 添加训练计划
	* @param uid ture int 会员ID
	* @param name ture char 名称
	* @param ranzhi ture char 目标燃脂量(千卡)
	* @param sports ture array 模块组合[{type:yinshi|dongzuo|kecheng,info:array|text,stime:08:00}]
	* @return 
	*/
	//end
	function add(){
		$user_id = $this->user_id;
		
		$uid = $this->input->post('uid',null,0);
		$sports = $this->input->post('sports',null,'');
		
		if(empty($sports)){
			$this->err_out('动作组合不能为空');
		}
		
		$sports = json_decode($sports,true);
		if(!$sports){
			$this->err_out('数据错误');
		}
		
		if(count($sports)<=0){
			$this->err_out('请选择动作组合');
		}
		
		$act_arr = array();
		foreach($sports as $key=>$val){
			if(!isset($val['type']) || !isset($val['info']) || !isset($val['stime'])){
				continue;
			}
			if(count($val['info'])<=0){
				continue;
			}
			if(!in_array($val['type'],array('yinshi','dongzuo','kecheng'))){
				continue;
			}
			
			$act_arr[] = $val;
		}
		if(count($act_arr)<=0){
			$this->err_out('动作组合有误');
		}
		
		
		$this->load->model('User/MemberModel','member');
		$user = $this->member->get_info($uid);
		if(!$user){
			$this->err_out('会员不存在');
		}
		
		$time = time();
		
		$arr = array(
			'user_id'=>$uid,
			'coach_id'=>$user_id,
			'name'=>$this->input->post('name',null,''),
			'target'=>$this->input->post('ranzhi',null,''),
			'addtime'=>$time
		);
		
		$ver_arr = array(
			'name'=>"请输入训练计划名称",
			'target'=>"请输入训练目标",
		);
		
		$result = $this->CheckData($ver_arr,$arr);
		
		if($result){
			$this->err_out($result);
		}
		
		$this->load->model('Sport/CourseTrainModel','CourseTrain');
		
		$is_exits = $this->CourseTrain->name_is_exits($arr['name']);
		if($is_exits){
			$this->err_out('该名称已经存在');
		}
		
		$tid = $this->CourseTrain->add($arr);
		$arr['id'] = $tid;

		$this->load->model('Sport/CourseTrainInfoModel','CourseTrainInfo');
		foreach($act_arr as $key=>$val){
			
			
			$info = $val['info'];
			if($val['type']=='dongzuo'){
				$arr = array();
				foreach($info as $v){
					$arr[] = array(
						'id'=> isset($v['id'])?$v['id']:0,
						'rest'=> isset($v['rest'])?$v['rest']:0,
						'duration'=> isset($v['duration'])?$v['duration']:0,
						'frequency'=> isset($v['frequency'])?$v['frequency']:0,
						'group'=> isset($v['group'])?$v['group']:0,
					);
				}
				$info = $arr;
			}
			
			$ls_arr = array(
				'tid'=>$tid,
				'stime'=>$val['stime'],
				'type'=>$val['type'],
				'info'=>json_encode($info)
			);
			$this->CourseTrainInfo->add($ls_arr);
		}
		$arr['sports'] = $act_arr;
		
		$this->data_out($arr);
	}
	
	
}
